﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FOBVT.Web.MainClass;
using System.Collections;

namespace FOBVT.Web.Admin.Pages
{
	public partial class EditNew : System.Web.UI.Page
	{
		private FOBTVNews _info;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!base.IsPostBack)
			{
				CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
				_FileBrowser.BasePath = "/ckfinder/";
				_FileBrowser.SetupCKEditor(this.txtNewsContent);
				InitPageData();
			}
		}

		protected void CheckAction(string action)
		{
			if (!ParamHandler.IsLegalForString(action) || (action != "add" && action != "edit"))
			{
				Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
			}
		}
		protected void CheckIntType(string param)
		{
			if (!ParamHandler.IsLegalForInt(param))
			{
				Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
			}
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			base.Response.Redirect("~/Admin/Pages/ListNews.aspx?$_k=" + new Random().NextDouble());
		}

		/// <summary>
		/// 初始化页面数据
		/// </summary>
		private void InitPageData()
		{
			CheckAction(Action);
			if (Action == "edit")
			{
				CheckIntType(NewsID);
				FOBTVNews news = CurrentNews;
				this.txtNewsContent.Text = news.NewsContent;
				this.ddlType.Text = news.NewsType;
				this.txtNewsTitle.Text = news.NewsTitle;
				this.txtPublisher.Text = news.Publisher;
				this.ckbIsRecommend.Checked = news.IsRecommend;
				this.txtNewsCategory.Text = news.NewsCategory;
				this.txtNewsSource.Text = news.NewsSource;
				this.txtIndex.Text = news.NewsIndex.ToString();

			}
			else if (Action == "add")
			{
				this.txtPublisher.Text = GetUserName;//从session读数据
			}
		}

		protected void BtnEditArticle_Click(object sender, EventArgs e)
		{
			string newsTitle = this.txtNewsTitle.Text.Trim();
			string publisher = this.txtPublisher.Text.Trim();
			string newsContent = this.txtNewsContent.Text.Trim();
			string newsCategory = this.txtNewsCategory.Text.Trim();
			if (string.IsNullOrEmpty(newsTitle))
			{
				this.LblError.Text = "<font color='red'>新闻标题不能为空！</font>";
			}
			else if (string.IsNullOrEmpty(newsCategory))
			{
				this.LblError.Text = "<font color='red'>新闻分类不能为空！</font>";
			}
			else if (string.IsNullOrEmpty(publisher))
			{
				this.LblError.Text = "<font color='red'>发布人不能为空！</font>";
			}
			else if (string.IsNullOrEmpty(newsContent))
			{
				this.LblError.Text = "<font color='red'>新闻内容不能为空！</font>";
			}
			else
			{
				System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\DB\\FOBVTweb.mdb"));
				FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
				ArrayList sql = new ArrayList();
				sql.Clear();


				FOBTVNews news = CurrentNews;
				news.NewsContent = newsContent;
				news.NewsTitle = newsTitle;
				news.NewsType = this.ddlType.Text;
				news.NewsSource = this.txtNewsSource.Text;
				news.Publisher = publisher;
				news.IsRecommend = this.ckbIsRecommend.Checked;
				news.NewsIndex = Convert.ToInt32(this.txtIndex.Text.Trim());
				news.NewsCategory = this.txtNewsCategory.Text.Trim();
				news.PublishDate = DateTime.Now;
				string isRecommend = news.IsRecommend == true ? "1" : "0";

				if (Action == "edit")
				{
					news.ID = int.Parse(NewsID);

					sql.Add("UPDATE  News SET NewsTitle='" + news.NewsTitle +
					"',NewsContent='" + news.NewsContent +
					"',NewsType='" + news.NewsType +
					"',NewsSource='" + news.NewsSource +
					"',NewsCategory='" + news.NewsCategory +
					"',Publisher='" + news.Publisher +
					"',IsRecommend='" + isRecommend +
					"',NewsIndex='" + news.NewsIndex +
					"' WHERE (ID =" + news.ID + ")");
				}
				else if (Action == "add")
				{
					sql.Add("INSERT INTO News ( NewsTitle,NewsContent,NewsType,NewsSource,NewsCategory,Publisher,IsRecommend,NewsIndex,PublishDate) VALUES ('" +
						news.NewsTitle + "','" +
						news.NewsContent + "','" +
						news.NewsType + "','" +
						news.NewsSource + "','" +
						news.NewsCategory + "','" +
						news.Publisher + "','" +
						isRecommend + "','" +
						news.NewsIndex + "','" +
						System.DateTime.Now.ToString() + "')");
				}
				if (DBClassTmp.ExcuteSQL(sql, Conn))
				{
					if (Action == "edit")
					{
						//FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Modify successfully!!");
						//ClientScript.RegisterStartupScript(GetType(), "SUBMITTED", "alert('Modify successfully!');window.returnValue=true;window.close();", true);
                        FOBVT.Web.MainClass.WebMessageBox.ShowAndRedirect(this.Page, "Modify successfully !", "ListNews.aspx");
                    }
					else
					{
                        FOBVT.Web.MainClass.WebMessageBox.ShowAndRedirect(this.Page, "Add successfully !", "ListNews.aspx");
						
					}

					//Response.Redirect("ListNews.aspx");
				}
				else
				{
					FOBVT.Web.MainClass.WebMessageBox.Show(this.Page, "Operation Failed!!");
				}
			}
		}

		protected string GetUserName
		{
			get
			{
				return FOBVT.Web.MainClass.SessionState.CurrentUser.FirstName + " " + FOBVT.Web.MainClass.SessionState.CurrentUser.LastName;
			}
		}

		protected string NewsID
		{
			get
			{
				return base.Request["newsId"];
			}
		}

		protected string Action
		{
			get
			{
				return base.Request["action"];
			}
		}

		protected FOBTVNews CurrentNews
		{
			get
			{
				FOBTVNews info = null;
				if (this._info != null)
				{
					info = this._info;
				}
				if (info == null)
				{
					if (ParamHandler.IsLegalForInt(NewsID))
					{
						string strSQL = "SELECT * from News WHERE ID=" + NewsID;

						System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\DB\\FOBVTweb.mdb"));
						FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();

						DataSet ds = DBClassTmp.GetDataSet(strSQL, Conn);
						info = new FOBTVNews();
						if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
						{
							info.ID = Convert.ToInt64(ds.Tables[0].Rows[0]["ID"].ToString());
							info.NewsTitle = ds.Tables[0].Rows[0]["NewsTitle"].ToString();
							info.NewsContent = ds.Tables[0].Rows[0]["NewsContent"].ToString();
							info.NewsType = ds.Tables[0].Rows[0]["NewsType"].ToString();
							info.NewsSource = ds.Tables[0].Rows[0]["NewsSource"].ToString();
							info.NewsCategory = ds.Tables[0].Rows[0]["NewsCategory"].ToString();
							info.Publisher = ds.Tables[0].Rows[0]["Publisher"].ToString();
							info.IsRecommend = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsRecommend"].ToString());
							info.NewsIndex = Convert.ToInt32(ds.Tables[0].Rows[0]["NewsIndex"].ToString());
							info.PublishDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["PublishDate"].ToString());
							return info;
						}
						else
						{
							return null;
						}
					}
					else
					{
						info = new FOBTVNews();
					}
					this._info = info;
				}
				return info;
			}
		}
	}
}